package cn.xdwanj.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

class Solution {
  public String minimizeResult(String expression) {
    int[] twoInt = getTwoInt(expression);
    int OriginSum = twoInt[0] + twoInt[1];

    List<Integer> front = new ArrayList<>();
    List<Integer> back = new ArrayList<>();
    List<Integer> resultList = new ArrayList<>();

    IntToList(front, twoInt[0]);
    IntToList(back, twoInt[1]);

    resultList.add(OriginSum);
    for (int i = 0; i < front.size(); i++) {
      for (int j = back.size() - 1; j >= i; j--) {
        int frontSub = getIntByIndex(front, i, true);
        int backSub = getIntByIndex(back, j, false);
        int frontPlus = getIntByIndex(front, i + 1, false);
        int backPlus = getIntByIndex(back, j - 1, true);

        resultList.add(frontSub * backSub + (frontPlus + backPlus));
      }
    }
    resultList.sort((o1, o2) -> {
      int i = o1;
      int j = o2;
      return Math.max(i, j);
    });

    return null;
  }

  //
  public int getIntByIndex(List<Integer> list, int index, boolean isPositive) {
    if (isPositive) {
      return ListToInt(
          list.subList(0, index)
      );
    } else {
      return ListToInt(
          list.subList(index, list.size())
      );
    }
  }

  // v
  public void IntToList(List<Integer> list, int num) {
    String s = String.valueOf(num);
    for (int i = 0; i < s.length(); i++) {
      list.add(
          Integer.valueOf(String.valueOf(s.charAt(i)))
      );
    }
  }

  // v
  public int ListToInt(List<Integer> list) {
    StringBuilder sb = new StringBuilder();
    for (Integer i : list) {
      sb.append(i);
    }

    return Integer.parseInt(sb.toString());
  }

  // v
  public int[] getTwoInt(String s) {
    String front = s.substring(0, s.indexOf('+'));
    String back = s.substring(s.indexOf('+'));

    return new int[]{Integer.parseInt(front), Integer.parseInt(back)};
  }

  public static void main(String[] args) {
    Solution solution = new Solution();

    int value = solution.getIntByIndex(List.of(2, 4, 7), 2, true);
    System.out.println("value = " + value);
  }

}

class Solution2 {

  private HashMap<Integer, List<Integer>> map;
  private Random random;

  public Solution2(int[] nums) {
    random = new Random();
    map = new HashMap<>();

    for (int i = 0; i < nums.length; i++) {
      if (map.get(nums[i]) == null) {
        map.put(nums[i], new ArrayList<Integer>(List.of(i)));
      } else {
        List<Integer> list = map.get(nums[i]);
        list.add(i);
        map.put(nums[i], list);
      }
    }
  }

  public int pick(int target) {
    List<Integer> list = map.get(target);
    int i = random.nextInt(list.size());
    return list.get(i);
  }
}



